<?php
// 配置文件
define('TODO_FILE', 'todos.json');
define('UPLOAD_DIR', 'upload/');

// 读取现有数据
$todos = [];
if (file_exists(TODO_FILE)) {
    $todos = json_decode(file_get_contents(TODO_FILE), true) ?: [];
}

// 处理删除请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['date'])) {
    $deleteDate = $_POST['date'];
    $todos = array_filter($todos, function($item) use ($deleteDate) {
        return $item['date'] !== $deleteDate;
    });
    
    // 保存修改后的数据
    file_put_contents(TODO_FILE, json_encode($todos, JSON_PRETTY_PRINT));
    header('Location: index.php');
    exit;
}

// 获取所有不同的日期
$dates = array_unique(array_column($todos, 'date'));
rsort($dates); // 按日期倒序排列
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        .timer-btn {
            padding: 12px 24px;
            font-size: 1em;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            display: block;
            margin-bottom: 25px;
        }
        .timer-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0,0,0,0.15);
        }
        .timer-btn:active {
            transform: translateY(0);
        }
        .button-container {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 25px;
        }
    </style>
    <title>删除历史作业</title>
    <style>
        body { font-family: sans-serif; margin: 20px; }
        h1 { font-size: 2em; margin-bottom: 20px; }
        .date-item { margin: 10px 0; padding: 10px; background: #f5f5f5; }
        button { padding: 5px 10px; background: #ff4444; color: white; border: none; cursor: pointer; }
        a { 
            display: inline-block;
            margin-top: 20px;
            color: #333;
            text-decoration: none;
            font-size: 1.2em;
        }
    </style>
</head>
<body>
    <h1>删除历史作业</h1>
    <div class="button-container">
        <a href="index.php" class="timer-btn">返回首页</a>
    </div>
    <?php if (empty($dates)): ?>
        <p>没有可删除的历史记录</p>
    <?php else: ?>
        <?php foreach ($dates as $date): ?>
            <div class="date-item">
                <form method="post" style="display: inline;">
                    <span><?= htmlspecialchars($date) ?></span>
                    <input type="hidden" name="date" value="<?= htmlspecialchars($date) ?>">
                    <button type="submit">删除</button>
                </form>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
    <br>
</body>
</html>